﻿#region Using Statements
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace RhinoXNA
{
    public class ModelEntity : IRhinoRenderable
    {
        private Model _model;
        
        private BoundingSphere _coll;

        public BoundingSphere CollisionSphere
        {
            get { return _coll; }
        }

        public ModelEntity Model
        {
            get { return this; }
        }

        public Model ActualModel
        {
            get { return _model; }
        }

        public ModelEntity(SpriteBatch sBatch, PlayerCamera cam, float aspectRatio, Model modelToLoad, Vector3 position, Vector3 scale, float boundingRadius)
            :base(sBatch, cam, aspectRatio)
        {
            _model = modelToLoad;
            this.Position = position;
            this.Scale = scale;
            _coll = new BoundingSphere(this.Position, boundingRadius);
        }

        public override void Initialize(string fileToLoad)
        {
            
        }

        public override void Update(GameTime gameTime)
        {
            _coll = new BoundingSphere(this.Position, _coll.Radius);
        }

        public override void Render()
        {
            Matrix[] transforms = new Matrix[_model.Bones.Count];
            _model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in _model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Begin();

                    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                    {
                        pass.Begin();

                        effect.EnableDefaultLighting();
                        //effect.Parameters["AmbientLightColor"].SetValue(new Vector3(0.01f, 0.01f, 0.01f));
                        //effect.Parameters["EmissiveColor"].SetValue(Vector3.Zero);

                        //effect.DirectionalLight0.Enabled = false;
                        //effect.DirectionalLight1.Enabled = true;
                        //effect.DirectionalLight1.DiffuseColor = new Vector3(1.0f, 0.5f, 0.3f);
                        //effect.DirectionalLight1.SpecularColor = Vector3.Zero;
                        //effect.DirectionalLight2.Enabled = true;
                        //effect.DirectionalLight2.SpecularColor = Vector3.Zero;
                        effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(this.Scale)
                            * Matrix.CreateRotationX(this.Rotation.X) * Matrix.CreateRotationY(this.Rotation.Y) * Matrix.CreateRotationZ(this.Rotation.Z)
                            * Matrix.CreateTranslation(this.Position);
                        effect.View = Matrix.CreateLookAt(_cam.Position, _cam.LookAt, Vector3.Up);
                        effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), _aspectRatio, 1.0f, 100000.0f);

                        pass.End();
                    }

                    effect.End();
                }

                mesh.Draw();
            }
        }
    }
}
